package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.charpter.ChapterVo;
import com.atguigu.eduservice.entity.charpter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-21
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询课程里所有的章节
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_Id",courseId);
        List<EduChapter> eduChapterList=baseMapper.selectList(wrapper);
        //2 根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapper1=new QueryWrapper<>();
        wrapper.eq("course_Id",courseId);
        List<EduVideo> eduVideoList=videoService.list(wrapper1);
        //3.遍历查询章节list集合进行封装
        List<ChapterVo> chapterVoList=new ArrayList<>();
        for(EduChapter chapter:eduChapterList){
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVoList.add(chapterVo);
            //4.遍历查询小节list集合，进行封装
            List<VideoVo> videoVoList=new ArrayList<>();
            for(EduVideo edu:eduVideoList){
                //判断：小节里面chapterid和章节里面id是否一样
                if(edu.getChapterId().equals(chapter.getId())){
                    //进行封装
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(edu,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }
        return chapterVoList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据小节中是否有chapterid章节id查询小节表，如果能查询出数据，就不进行删除
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count=videoService.count(wrapper);//这里是查询表中与chapterId相等的记录数量
        if(count>0){//查询出小节，不进行删除
            throw new GuliException(20001,"带有小节不能删除");
        }else{
            //删除章节
            int result=baseMapper.deleteById(chapterId);
            return result>0;//如果result>0的话返回true
        }
    }

    //2.根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
